package com.juli.v2xdemo.tcp.tcp_impl;

import android.os.Message;
import android.text.TextUtils;

import com.cz.basetool.CodeTool;
import com.cz.basetool.ui_work.util.log.AndroidConsoleLogPrinter;

import com.example.tcpmaster.AbsSocketTransceiver;
import com.example.tcpmaster.TcpClient;
import com.juli.v2xdemo.tcp.protocol.ProtocolDispatcher;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/**
 * Created by lianxiang on 2017/9/7.
 */

public class SocketTransceiver extends AbsSocketTransceiver {
    private byte[] bytesReadBuf = new byte[1024];
    private byte[] frameDatas = null;
    private int nLen;
    private String aFrameData = "";

    private static final String TAG = "SocketTransceiver";
    private static final int CALL_WHAT = 0;
    private final byte[] bufferData;


    /**
     * 消息数据的存放
     */
    private StringBuilder bufferReadMsg = new StringBuilder();
/*
    private final Handler msgHandler;


    private int MSG_TYPE_RECEIVE = 10;

    private class MsgCallback implements Handler.Callback {

        @Override
        public boolean handleMessage(Message msg) {
            step1_get_wholeFrame(msg.obj.toString());
            aFrameData = "";
            return true;
        }
    }
*/


    public SocketTransceiver(TcpClient tcpClient, Socket socket) {
        super(tcpClient, socket);
        bufferData = new byte[1024];
        bufferReadMsg.setLength(0);
/*

        HandlerThread handlerThread = new HandlerThread("SocketTransceiver HandlerThread");
        handlerThread.start();

        msgHandler = new Handler(handlerThread.getLooper(), new MsgCallback());*/

    }

    @Override
    public void onRead(InputStream in) throws IOException {
        try {
            nLen = in.read(bytesReadBuf);
            if (nLen != -1) {
                frameDatas = new byte[nLen];
                System.arraycopy(bytesReadBuf, 0, frameDatas, 0, nLen);
                aFrameData = CodeTool.bytes2HexString(frameDatas);
                AndroidConsoleLogPrinter.e("SocketTransceiver", "接收<< " + aFrameData);
                //在这里做分包判断
                if (!TextUtils.isEmpty(aFrameData)) {
                    ProtocolDispatcher.getInstance().dispatch(aFrameData);
                    aFrameData = "";
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onHandleMessage(Message message) {
        return false;
    }

 /*

    private void findOneFrame(String whole7EData){
        Log.d(TAG, "findOneFrame: " + whole7EData);
        String realMsg = whole7EData.substring(2, whole7EData.length() - 2);
        String deEscape7EString = CodeTool.getDeEscape7EString(realMsg);
        sendMessage(CALL_WHAT,deEscape7EString);
    }


    @Override
    public boolean onHandleMessage(Message message) {
        switch (message.what) {
            case CALL_WHAT:
                onReceive(message.obj.toString());
                break;
        }
        return true;
    }*/
}
